﻿// Write a program that finds in given array of integers a sequence of given sum S (if present). Example:	 {4, 3, 1, 4, 2, 5, 8}, S=11  {4, 2, 5}	


using System;


namespace SequenceOfGivvenSum
{
    class SequenceOfGivvenSum
    {
        static void Main(string[] args)
        {
            Console.Write("Enter N: ");
            int n = int.Parse(Console.ReadLine());
            Console.Write("Enter S: ");
            int s = int.Parse(Console.ReadLine());
            int[] numbers = new int[n];
            int currentSum = 0;
            int startPosition = 0;
            int endPosition = 0;
            bool isThereSuchSum = false;

            for (int i = 0; i < n; i++)
            {
                Console.Write("Enter element value: ");
                numbers[i] = int.Parse(Console.ReadLine());     //filling the array with values
            }

            for (int i = 0; i < numbers.Length; i++)
            {
                currentSum = numbers[i];
                for (int j = i + 1; j < numbers.Length; j++)
                {
                    currentSum += numbers[j];
                    if (currentSum == s)
                    {
                        isThereSuchSum = true;
                        startPosition = i;
                        endPosition = j;
                    }
                }
            }
            if (isThereSuchSum == false)    //check if in the array is such sum [S]
            {
                Console.WriteLine("The sum is not present in this array!");
            }
            if (isThereSuchSum == true)
            {
                Console.Write("{ ");
                for (int i = startPosition; i <= endPosition; i++)  //print the result
                {
                    Console.Write("{0} ", numbers[i]);
                }
                Console.WriteLine("}");
            }
        }
    }
}
